import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

# ----------向上开口的曲线----------------
x = np.linspace(-5,5,20)
y = x ** 2 + 1

fig,axs = plt.subplots()
axs.plot(x,y,'r-')

#-----------在曲线中直到任意一点------------
x_current = tf.Variable(-3.0)
y_current = x_current ** 2 + 1

# print(x_current.numpy())
# print(y_current.numpy())

axs.plot(x_current.numpy(),y_current.numpy(),'bo')

#----------绘制经过该点的切线---------------
x_tmp = x_current
with tf.GradientTape() as tape:
    y_tmp = x_tmp**2 + 1
    w = tape.gradient(y_tmp,x_tmp)
    b = y_tmp - w * x_tmp

    w = w.numpy()
    b = b.numpy()

    axs.plot(x,w * x + b,'g--')

plt.show()